package edu.hawaii.ics.pjdb.worker;

import java.sql.SQLException;

import org.apache.thrift.TException;

import edu.hawaii.ics.pjdb.types.WorkerCommand;
import edu.hawaii.ics.pjdb.types.WorkerService.Iface;
import edu.hawaii.ics.pjdb.worker.command_runners.CommandRunner;

/**
 * Responsible for receiving commands from the master, executing them, and
 * reporting the results to the output socket.
 * 
 */
public class WorkerCommandHandler implements Iface {

    protected final CommandRunner.Switch cmdSwitch;

    public WorkerCommandHandler() {
        this.cmdSwitch = new CommandRunner.Switch();
    }

    @Override
    public void executeCommand(WorkerCommand command) throws TException {
        try {
            cmdSwitch.execute(command);
        } catch (SQLException e) {
            throw new TException("Unable to execute command on local db:", e);
        }
    }
}
